class Solution(object):
    def largestTimeFromDigits(self, arr):
        self.max_time = ''
        out = []
        p = [0] * 4

        def judge():
            if out[0] * 10 + out[1] <= 23 and out[2] * 10 + out[3] <= 59:
                return str(out[0]) + str(out[1]) + ':' + str(out[2]) + str(out[3])
            else:
                return ''

        def search(k):
            if k == 4:
                self.max_time = max(self.max_time, judge())
            else:
                for i in range(4):
                    if not p[i]:
                        p[i] = 1
                        out.append(arr[i])
                        search(k + 1)
                        p[i] = 0
                        out.pop()

        search(0)
        return self.max_time
